<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class withdraw extends core {
	private $withdraw_mod;

	public function __construct() {
		parent::__construct();
		$this->withdraw_mod = m("withdraw");
	}

	//列表
	public function index() {
		$where = '';
		$status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$withdraw_id = (isset($_GET['withdraw_id'])) ? trim($_GET['withdraw_id']) : '';
		$begin_date = (isset($_GET['begin_date'])) ? trim($_GET['begin_date']) : '';
		$end_date = (isset($_GET['end_date'])) ? trim($_GET['end_date']) : '';
		if (strlen($begin_date)) {
			$where .= " AND w.add_time>='".strtotime($begin_date)."'";
		}
		if (strlen($end_date)) {
			$where .= " AND w.add_time<='".strtotime($end_date)."'";
		}
		if ($keyword) {
			$where .= "AND (m.name LIKE '%{$keyword}%' OR w.bank_name LIKE '%{$keyword}%' OR w.bank_branch LIKE '%{$keyword}%' 
				OR w.bank_card LIKE '%{$keyword}%' OR w.alipay LIKE '%{$keyword}%')";
		}
		if (strlen($status)) {
			$where .= "AND w.status='{$status}'";
		}
		if (strlen($withdraw_id)) {
			$where .= "AND w.id='{$withdraw_id}'";
		}
		//分页
		$this->setpages(array('withdraw_id'=>$withdraw_id, 'keyword'=>$keyword, 'status'=>$status, 
			'begin_date'=>$begin_date, 'end_date'=>$end_date));

		$sql = "SELECT w.*, m.name as member_name, m.mobile  
			FROM jf_withdraw w 
			LEFT JOIN jf_member m ON w.member_id=m.id  
			WHERE 1 {$where} ORDER BY w.id DESC";
		//echo $sql;
		$rs = $this->ezr->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]['status_name'] = $this->withdraw_mod->status_name($row['status']);
			}
		}
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('status',$status);
		$this->smarty->assign('withdraw_id',$withdraw_id);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('begin_date',$begin_date);
		$this->smarty->assign('end_date',$end_date);
		$this->smarty->assign('where',$where);

		$this->display();
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$withdraw_id = (isset($_GET['withdraw_id']) && intval($_GET['withdraw_id'])) ? intval($_GET['withdraw_id']) : 0;
			$sql = "SELECT w.*, m.name as member_name FROM jf_withdraw w 
				LEFT JOIN jf_member m ON w.member_id=m.id  
				WHERE w.id='{$withdraw_id}'";
			$withdraw = $this->db->get_row($sql);
			if ($withdraw) {
				$withdraw->status_name = $this->withdraw_mod->status_name($withdraw->status);
			}
			$this->smarty->assign('row', $withdraw);
			$this->display();
		}
	}

	//保存
	public function save() {
		$audit_adminid = $_SESSION['admin']->id;
		$audit_admin = $_SESSION['admin']->name;
		$withdraw_id = (isset($_POST['withdraw_id']) && intval($_POST['withdraw_id'])) ? intval($_POST['withdraw_id']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$audit_memo = (isset($_POST['audit_memo']) && trim($_POST['audit_memo'])) ? trim($_POST['audit_memo']) : '';
		$withdraw_money = (isset($_POST['withdraw_money']) && trim($_POST['withdraw_money'])) ? trim($_POST['withdraw_money']) : '';

		$sql = "UPDATE jf_withdraw SET status='{$status}', audit_memo='{$audit_memo}', audit_time='".time()."' ,
				audit_adminid='{$audit_adminid}',audit_admin='{$audit_admin}'
				WHERE id='{$withdraw_id}' ";
		$this->db->query($sql);

		// 更新可提现的金额，以及发送通知消息
		$sql = "SELECT commission, withdraw_money, udid, member_id 
			FROM jf_withdraw w INNER JOIN jf_member m ON w.member_id=m.id WHERE w.id='{$withdraw_id}'";
		//echo $sql;
		$row = $this->db->get_row($sql);
		if ($row) {
			switch ($status) {
				case 1:
					if ($row->commission>0) {
						if ($row->commission >= $row->withdraw_money) {
							$remain_income = $row->commission - $row->withdraw_money;
						} else {
							$remain_income = 0;
						}
						$sql = "UPDATE jf_member SET commission='{$remain_income}' WHERE id='{$row->member_id}'";
						$this->db->query($sql);
						if ($row->udid) $this->notify->send($row->udid, "恭喜您，您的提现申请已经审核并发放。");
						//echo $row->udid;
					}
					break;
				case -1:
					if ($row->udid) $this->notify->send($row->udid, "您的提现申请被拒绝：{$audit_memo}。");
					$remain_income = $row->commission + $row->withdraw_money;
					$sql = "UPDATE jf_member SET commission='{$remain_income}' WHERE id='{$row->member_id}'";
					$this->db->query($sql);
					break;
			}
		}
		//echo $withdraw_id;
		//exit;
		header("Location:?app=withdraw&index");
	}

	//删除商品
	public function delete() {
		$withdraw_id = (isset($_GET['withdraw_id']) && trim($_GET['withdraw_id'])) ? intval($_GET['withdraw_id']) : 0;
		$sql = "DELETE FROM jf_withdraw WHERE id='{$withdraw_id}'";
		$this->db->query($sql);
		header("Location:?app=withdraw&act=index");
	}

	//导出
	public function export() {
		$fields = array(
			'id'=>'ID',
			'member_name'=>'会员',
			'bank_name'=>'银行名称',
			'bank_branch'=>'支行名称',
			'bank_card'=>'银行卡号',
			'alipay'=>'支付宝',
			'withdraw_money'=>'提现金额',
			'add_time'=>'申请日期',
			'status_name'=>'状态',
			'audit_time'=>'审核日期',
			'audit_memo'=>'审核备注',
		);
		$where = (isset($_GET['where']) && trim($_GET['where'])) ? trim($_GET['where']) : '';
		$where = stripcslashes($where);
		$sql = "SELECT w.*, m.name as member_name 
			FROM jf_withdraw w 
			LEFT JOIN jf_member m ON w.member_id=m.id  
			WHERE 1 {$where} ORDER BY w.id DESC";
		//echo $sql;
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $key => $row) {
				$rs[$key]->status_name = $this->withdraw_mod->status_name($row->status);
				if ($row->add_time) $rs[$key]->add_time = date("Y-m-d H:i:s", $row->add_time);
				if ($row->audit_time) $rs[$key]->audit_time = date("Y-m-d H:i:s", $row->audit_time);
			}
			export_excel($rs, $fields, "withdraw");
		}
	}
}
